﻿namespace AgileBoard.Domain
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Security.Principal;
    using Iesi.Collections.Generic;
    using SharpArch.Core.DomainModel;

    public partial class User : Entity, IPrincipal
    {
        private ISet<Feelings> _feelings = new HashedSet<Feelings>();

        public static User Guest
        {
            get { return new User() {Email = "Guest@guest.com", Role = Role.Guest}; }
        }

        public virtual string PublicIdentity
        {
            get
            {
                if (CanLogin) return Email;
                return "Guest";
            }
        }

        public virtual bool CanLogin
        {
            get { return IsAdministrator || IsLeadDeveoper; }
        }

        public virtual bool IsAdministrator
        {
            get { return Role.RoleID == Role.AdministratorId; }
        }

        public virtual bool IsLeadDeveoper
        {
            get { return Role.RoleID == Role.LeadDeveloperId; }
        }

        public virtual bool IsDeveloper
        {
            get { return Role.RoleID == Role.DeveloperId; }
        }

        /// <summary>
        /// Create Time
        /// </summary>
        public virtual DateTime CreateTime { get; set; }

        /// <summary>
        /// Creator
        /// </summary>
        public virtual string Creator { get; set; }


        /// <summary>
        /// EmailID
        /// </summary>
        public virtual string Email { get; set; }

        /// <summary>
        /// User ID
        /// </summary>
        /// 
        public virtual Guid UserID { get; set; }

        /// <summary>
        /// User role
        /// </summary>
        /// 
        public virtual Role Role { get; set; }

        /// <summary>
        /// Project
        /// </summary>
        /// 
        public virtual Project Project { get; set; }


        /// <summary>
        /// Mobile
        /// </summary>
        public virtual string Mobile { get; set; }

        /// <summary>
        /// Password
        /// </summary>
        public virtual string Password { get; set; }

        /// <summary>
        /// Telephone
        /// </summary>
        public virtual string Telephone { get; set; }

        /// <summary>
        /// Username
        /// </summary>
        public virtual string UserName { get; set; }

        /// <summary>
        /// Defaultuser
        /// </summary>
        public virtual bool DefaultUser { get; set; }

        /// <summary>
        /// ValidFrom
        /// </summary>
        public virtual DateTime? ValidFrom { get; set; }


        /// <summary>
        /// ValidTo
        /// </summary>
        public virtual DateTime? ValidTo { get; set; }

        public virtual ReadOnlyCollection<Feelings> Feelings
        {
            get { return new List<Feelings>(_feelings).AsReadOnly(); }
        }

        #region IPrincipal Members

        public virtual IIdentity Identity
        {
            get
            {
                bool isAuthenticated = !(Role.Name == Role.Guest.Name);
                return new Identity(isAuthenticated, Email);
            }
        }

        public virtual bool IsInRole(string role)
        {
            return Role.Name == role;
        }

        #endregion

        public virtual void AddFeeling(Feelings feeling)
        {
            feeling.UserID = UserID.ToString();
            _feelings.Add(feeling);
        }

        public virtual void RemoveFeeling(Feelings n)
        {
        }
    }

    /// <summary>
    /// Simple implementation of IIdentity
    /// </summary>
    public class Identity : IIdentity
    {
        private bool isAuthenticated;
        private string name;

        public Identity(bool isAuthenticated, string name)
        {
            this.isAuthenticated = isAuthenticated;
            this.name = name;
        }

        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated
        {
            get { return isAuthenticated; }
        }

        public string Name
        {
            get { return name; }
        }

        #endregion
    }
}